// import { openDBvideo, getVideoUrl } from "@/util/vide.js";

chrome.runtime.onInstalled.addListener((installedDetails) => {
  if (installedDetails.reason === "update") {
    console.log("chrome extension update success");
  }
});

//给系统发送通知，然后监听的点击事件回调
chrome.notifications.onClicked.addListener(function (notificationId) {
  chrome.tabs.create({ url: "https://www.baidu.com" });
  chrome.notifications.clear(notificationId);
});

// // 拦截请求并修改请求头
// chrome.webRequest.onBeforeSendHeaders.addListener(
//   function (details) {
//     details.requestHeaders.push({ name: "X-Custom-Header", value: "hello" });
//     return { requestHeaders: details.requestHeaders };
//   },
//   { urls: ["<all_urls>"] },
//   ["blocking", "requestHeaders"]
// );

// 处理来自内容脚本的请求
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request.type === "chromeRequest") {
    fetch(request.config.url, {
      method: request.config.method,
      headers: request.config.headers,
      body: JSON.stringify(request.config.data),
    })
      .then(async (res) => ({
        data: await res.json(),
        status: res.status,
        headers: Object.fromEntries(res.headers.entries()),
      }))
      .then(sendResponse);
    return true; // 保持消息通道开放
  }
});

// background.js
// chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
//   if (request.action === "yourMethodName") {
//     const result = yourMethod();
//     sendResponse({ result: result });
//   }
// });

// function download(url) {
//   var options = {
//     url: url,
//   };
//   chrome.downloads.download(options);
// }
// // eslint-disable-next-line
// chrome.runtime.onMessage.addListener((Message, sender, sendResponse) => {
//   if (Message.Message === "down") {
//     download(Message.data);
//   }
// });
